/*
* Copyright (c) 2002 and later by MH Software-Entwicklung. All Rights Reserved.
*  
* JTattoo is multiple licensed. If your are an open source developer you can use
* it under the terms and conditions of the GNU General Public License version 2.0
* or later as published by the Free Software Foundation.
*  
* see: gpl-2.0.txt
* 
* If you pay for a license you will become a registered user who could use the
* software under the terms and conditions of the GNU Lesser General Public License
* version 2.0 or later with classpath exception as published by the Free Software
* Foundation.
* 
* see: lgpl-2.0.txt
* see: classpath-exception.txt
* 
* Registered users could also use JTattoo under the terms and conditions of the 
* Apache License, Version 2.0 as published by the Apache Software Foundation.
*  
* see: APACHE-LICENSE-2.0.txt
*/

package com.hitechbangla.fctu.jtattoo.plaf;

import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.metal.MetalToolTipUI;

/**
 * @author Michael Hagen
 */
public class BaseToolTipUI extends MetalToolTipUI {

    public static ComponentUI createUI(JComponent c) {
        return new BaseToolTipUI();
    }

    public void installUI(JComponent c) {
        super.installUI(c);
        if (DecorationHelper.isTranslucentWindowSupported() 
                && ToolTipManager.sharedInstance().isLightWeightPopupEnabled()) {
            c.setBorder(BorderFactory.createEmptyBorder(6, 12, 12, 12));
            c.setOpaque(false);
            Container parent = c.getParent();
            if (parent instanceof JPanel) {
                ((JPanel)c.getParent()).setOpaque(false);
            }
        } else {
            c.setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6));
        }
    }

    public void paint(Graphics g, JComponent c) {
        Graphics2D g2D = (Graphics2D)g;
        Composite savedComposit = g2D.getComposite();
        Object savedRederingHint = g2D.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
        g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        
        int w = c.getWidth();
        int h = c.getHeight();
        Color backColor = AbstractLookAndFeel.getTheme().getTooltipBackgroundColor();
        
        if (DecorationHelper.isTranslucentWindowSupported()
                && ToolTipManager.sharedInstance().isLightWeightPopupEnabled()) {
            // Make the popup transparent
            Container parent = c.getParent();
            while (parent != null) {
                if ((parent.getClass().getName().indexOf("HeavyWeightWindow") > 0) && (parent instanceof Window)) {
                    DecorationHelper.setTranslucentWindow((Window)parent);
                    break;
                }
                parent = parent.getParent();
            }

            // draw the shadow
            g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.04f));
            g2D.setColor(AbstractLookAndFeel.getTheme().getShadowColor());
            g2D.fillRoundRect(0, 6, w, h - 6, 12, 12);
            g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.02f));
            g2D.fillRoundRect(1, 7, w - 2, h - 8, 11, 11);
            g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.04f));
            g2D.fillRoundRect(2, 8, w - 4, h - 10, 10, 10);
            g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.06f));
            g2D.fillRoundRect(3, 9, w - 6, h - 12, 9, 9);
            g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.08f));
            g2D.fillRoundRect(4, 10, w - 8, h - 14, 8, 8);
            g2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.12f));
            g2D.fillRoundRect(5, 11, w - 10, h - 16, 7, 7);
            g2D.setComposite(savedComposit);
            
            // Draw background with borders
            if (ColorHelper.getGrayValue(backColor) < 128) {
                g2D.setColor(ColorHelper.brighter(AbstractLookAndFeel.getTheme().getBackgroundColor(), 20));
                g2D.fillRoundRect(6, 0, w - 13, h - 7, 6, 6);
                g2D.setColor(ColorHelper.darker(backColor, 40));
                g2D.drawRoundRect(6, 0, w - 13, h - 7, 6, 6);
                g2D.setColor(ColorHelper.darker(backColor, 10));
                g2D.drawRect(11, 5, w - 23, h - 17);
            } else {
                g2D.setColor(Color.white);
                g2D.fillRoundRect(6, 0, w - 13, h - 7, 6, 6);
                g2D.setColor(ColorHelper.darker(backColor, 40));
                g2D.drawRoundRect(6, 0, w - 13, h - 7, 6, 6);
                g2D.setColor(ColorHelper.darker(backColor, 10));
                g2D.drawRect(11, 5, w - 23, h - 17);
            }
            g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, savedRederingHint);
            // Draw the text. This must be done within an offscreen image because of a bug
            // in the jdk, wich causes ugly antialiased font rendering when background is
            // transparent and popup is heavy weight.
            BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            Graphics2D big = bi.createGraphics();
            big.setClip(0, 0, w, h);
            Paint savedPaint = big.getPaint();
            Color cHi;
            Color cLo;
            if (ColorHelper.getGrayValue(backColor) < 128) {
                cHi = ColorHelper.brighter(backColor, 10);
                cLo = ColorHelper.darker(backColor, 20);
            } else {
                cHi = ColorHelper.brighter(backColor, 40);
                cLo = ColorHelper.darker(backColor, 5);
            }
            big.setPaint(new GradientPaint(0, 6, cHi, 0, h - 18, cLo));
            big.fillRect(12, 6, w - 24, h - 18);
            big.setPaint(savedPaint);

            if (c instanceof JToolTip) {
                JToolTip tip = (JToolTip)c;
                if (tip.getComponent().isEnabled()) {
                    c.setForeground(AbstractLookAndFeel.getTheme().getTooltipForegroundColor());
                } else {
                    c.setForeground(AbstractLookAndFeel.getTheme().getDisabledForegroundColor());
                }
            }
            super.paint(big, c);
            g2D.setClip(12, 6, w - 24, h - 18);
            g2D.drawImage(bi, 0, 0, null);
            
        } else {
            // Draw background with borders
            if (ColorHelper.getGrayValue(backColor) < 128) {
                g2D.setColor(ColorHelper.brighter(AbstractLookAndFeel.getTheme().getBackgroundColor(), 20));
                g2D.fillRect(0, 0, w, h);
                g2D.setColor(ColorHelper.darker(backColor, 40));
                g2D.drawRect(0, 0, w - 1, h - 1);
                g2D.setColor(ColorHelper.darker(backColor, 10));
                g2D.drawRect(5, 5, w - 11, h - 11);
            } else {
                g2D.setColor(Color.white);
                g2D.fillRect(0, 0, w, h);
                g2D.setColor(ColorHelper.darker(backColor, 40));
                g2D.drawRect(0, 0, w - 1, h - 1);
                g2D.setColor(ColorHelper.darker(backColor, 10));
                g2D.drawRect(5, 5, w - 11, h - 11);
            }
            g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, savedRederingHint);
            
            Paint savedPaint = g2D.getPaint();
            Color cHi;
            Color cLo;
            if (ColorHelper.getGrayValue(backColor) < 128) {
                cHi = ColorHelper.brighter(backColor, 10);
                cLo = ColorHelper.darker(backColor, 20);
            } else {
                cHi = ColorHelper.brighter(backColor, 40);
                cLo = ColorHelper.darker(backColor, 5);
            }
            g2D.setPaint(new GradientPaint(0, 6, cHi, 0, h - 12, cLo));
            g2D.fillRect(6, 6, w - 12, h - 12);
            g2D.setPaint(savedPaint);
            
            super.paint(g, c);
        }
    }
    
}
